package com.surelution.projects.crm

import java.text.SimpleDateFormat

class GasCardRechargeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def htmlTableParserService
	
    def index = {
        redirect(action: "list", params: params)
    }

	def ImportExcelService
	def springSecurityService
	def cardRechargeImport = {
		def fromLine =1
		def file = params.gasCardRechargeFile
		int lineNum = 0
		def fileName = file.originalFilename
		ImportExcel excel = ImportExcelService.storeFile(file.inputStream,fileName)
		
		BufferedReader br = new BufferedReader(new InputStreamReader(file.inputStream, "utf8"))
		def rows = htmlTableParserService.parse(br)
		rows.each {row->
			if(lineNum > fromLine) {
				println row
				float tradeSum = Float.parseFloat(row[2])
				if(row.size() > 7) {
					GasCardRecharge gasCardRecharge = GasCardRecharge.createCriteria().get {
						eq("tradeNo", row[0])
					}
					if (!gasCardRecharge){
						gasCardRecharge = new GasCardRecharge()
						gasCardRecharge.tradeNo = row[0]
					}
					gasCardRecharge.tradeSum = tradeSum
					gasCardRecharge.tradeType = row[3]
					gasCardRecharge.gasCardNo = row[4]
					gasCardRecharge.marketName = row[5]  
					gasCardRecharge.openAgency = row[6]
					if (OwnStation.findByName(row[1])){
						gasCardRecharge.station = OwnStation.findByName(row[1])
					}
					else if (StationNameAlias.findByName(row[1])){
							gasCardRecharge.station = StationNameAlias.findByName(row[1]).station
						}
						else {
							flash.message = "${message(code: 'default.noFoundStationName.message', args:[row[1]])}"
							}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
					gasCardRecharge.tradeTime = sdf.parse(row[7])
					gasCardRecharge.save()
					flash.message = "${message(code: 'default.updateFileSuccess.message', args: [message(code: 'gasCardRecharge.label', default: 'gasCardRecharge')])}"
				}
			}
			lineNum++
		}
		excel.uploader = springSecurityService.principal.username
		excel.type = "加油卡充值明细"
		excel.save()
		redirect(action:"list")
	}
	
	
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def sdf = new SimpleDateFormat('yyyy-MM-dd')
		def dateFrom = params.dateFrom? sdf.parse(params.dateFrom):null
		def dateTo = params.dateTo? sdf.parse(params.dateTo):null
		def station = params['station.id']?OwnStation.get(params['station.id']):null
		def result = GasCardRecharge.createCriteria().list ([max:params.max, offset:params.offset]){
			if(station){
				eq("station",station)
			}
			if(dateFrom) {
				ge("tradeTime", dateFrom)
			}
			if(dateTo) {
				le("tradeTime", dateTo)
			}
			order("tradeTime","desc")
		}
        [gasCardRechargeInstanceList: result, gasCardRechargeInstanceTotal: result.totalCount]
    }

    def create = {
        def gasCardRechargeInstance = new GasCardRecharge()
        gasCardRechargeInstance.properties = params
        return [gasCardRechargeInstance: gasCardRechargeInstance]
    }

    def save = {
        def gasCardRechargeInstance = new GasCardRecharge(params)
        if (gasCardRechargeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), gasCardRechargeInstance.id])}"
            redirect(action: "show", id: gasCardRechargeInstance.id)
        }
        else {
            render(view: "create", model: [gasCardRechargeInstance: gasCardRechargeInstance])
        }
    }

    def show = {
        def gasCardRechargeInstance = GasCardRecharge.get(params.id)
        if (!gasCardRechargeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
            redirect(action: "list")
        }
        else {
            [gasCardRechargeInstance: gasCardRechargeInstance]
        }
    }

    def edit = {
        def gasCardRechargeInstance = GasCardRecharge.get(params.id)
        if (!gasCardRechargeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [gasCardRechargeInstance: gasCardRechargeInstance]
        }
    }

    def update = {
        def gasCardRechargeInstance = GasCardRecharge.get(params.id)
        if (gasCardRechargeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (gasCardRechargeInstance.version > version) {
                    
                    gasCardRechargeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge')] as Object[], "Another user has updated this GasCardRecharge while you were editing")
                    render(view: "edit", model: [gasCardRechargeInstance: gasCardRechargeInstance])
                    return
                }
            }
            gasCardRechargeInstance.properties = params
            if (!gasCardRechargeInstance.hasErrors() && gasCardRechargeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), gasCardRechargeInstance.id])}"
                redirect(action: "show", id: gasCardRechargeInstance.id)
            }
            else {
                render(view: "edit", model: [gasCardRechargeInstance: gasCardRechargeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def gasCardRechargeInstance = GasCardRecharge.get(params.id)
        if (gasCardRechargeInstance) {
            try {
                gasCardRechargeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardRecharge.label', default: 'GasCardRecharge'), params.id])}"
            redirect(action: "list")
        }
    }
}
